﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Common.Master" AutoEventWireup="true" CodeBehind="ArchiveTypeTreeManage.aspx.cs" Inherits="Web.Views.DataManage.ArchiveTypeTreeManage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
	<title></title>
    <script type="text/javascript" src="../../Javascript/json2.js"></script>
  <script type="text/javascript">
      var appendMenuList = new Array;
      var editMenuList = new Array;
      var removeMenuList = new Array;
      var status = '';
      $(function () {
          $('#treeArchiveMenu').height($('body').height() - 30);
      });
      $(function () {
          var timestamp = new Date().getTime();
          $('#treeArchiveMenu').tree({
              method: "get",
              lines: true,
              idFiled: "id",
              textFiled: "text",
              parentField: "pid",
              cascadeCheck: false,
              url: "../../HttpHandles/ArchiveHandle.ashx?archivemenu=yes&timestamp=" + new Date().getTime() + "",
              onBeforeExpand: function (node, param) {
                  alert('加载失败！');
              },
              onSelect: function (node) {
                  if (node) {
                      $('#menuText').val(node.text);
                      $('#menuCode').val(node.attributes.typeCode);
                      $('#menuTextValidateMsg').hide();
                      $('#menuCodeValidateMsg').hide();
                      $('#btnAppend').removeAttr('disabled');
                      $('#btnEdit').removeAttr('disabled');
                      $('#btnRemove').removeAttr('disabled');
                      $('#btnCommit').removeAttr('disabled');
                      $('#btnReject').removeAttr('disabled');
                      status = 'edit';
                  }
              }
          });
      });
      function append() {
          var node = $('#treeArchiveMenu').tree('getSelected');
          if (node == null) {
              alert('请先选择一个节点！');
              return;
          }
          $('#menuText').val('');
          $('#menuCode').val('');
          $('#btnAppend').attr('disabled', 'disabled');
          $('#btnEdit').attr('disabled', 'disabled');
          $('#btnRemove').attr('disabled', 'disabled');
          $('#btnCommit').attr('disabled', 'disabled');
          $('#btnReject').attr('disabled', 'disabled');
          status = 'append';          
      }
      function remove() {
          var node = $('#treeArchiveMenu').tree('getSelected');
          if (node == null) {
              alert('请先选择一个节点！');
              return;
          }
          var children = $('#treeArchiveMenu').tree('getChildren', node.target);
          if (children.length > 0) {
              alert('此编目下存在子节点或单井数据,请先删除其下的子节点或单井数据！');
              return;
          }
          $('#menuText').val('');
          $('#menuCode').val('');
          var pid = "0";
          var pnode = $('#treeArchiveMenu').tree('getParent', node.target);
          if (pnode != null) {
              pid = pnode.id;
          }
          $('#treeArchiveMenu').tree('remove', node.target);
          for (i = 0; i < appendMenuList.length; i++) {
              if (appendMenuList[i].WellcatalogId == node.id) {
                  appendMenuList.splice(i, 1);
                  return;
              }
          }
          for (i = 0; i < editMenuList.length; i++) {
              if (editMenuList[i].WellcatalogId == node.id) {
                  editMenuList.splice(i, 1);
                  break;
              }
          }
          var menu = new Object;
          menu.TypeID = node.id;
          menu.ArchiveMenu = node.text;
          menu.ParentID = pid;
          menu.TypeCode = node.attributes.typeCode;
          removeMenuList.push(menu);
          status = '';
          return;    
      }
      function commit() {
          var updateModel = new Object;
          updateModel.InsertedRows = appendMenuList;
          updateModel.UpdatedRows = editMenuList;
          updateModel.DeletedRows = removeMenuList;
          var postObj = new Object;
          postObj.updateModel = JSON.stringify(updateModel);
          var jsonStr;
          if (updateModel.InsertedRows.length >= 1 || updateModel.UpdatedRows.length >= 1 || updateModel.DeletedRows.length >= 1) {
              try {
                  jsonStr = JSON.stringify(postObj);
              }
              catch (e) {
                  alert(e.Message);
                  return;
              }
              $.ajax({
                  type: 'POST',
                  contentType: "application/json",
                  url: "DataManageServices.asmx/UpdataArchiveType",
                  data: jsonStr,
                  dataType: 'json',
                  success: function (event, XMLHttpRequest, ajaxOptions) {
                      appendMenuList.splice(0, appendMenuList.length);
                      editMenuList.splice(0, editMenuList.length);
                      removeMenuList.splice(0, removeMenuList.length);
                      $('#treeArchiveMenu').tree('reload');
                      status = '';
                      alert('更新成功！');
                      return;
                  },
                  error: function (event, XMLHttpRequest, ajaxOptions, thrownError) {
                      var message = '服务器错误！';
                      response = jQuery.parseJSON(event.responseText);
                      if (response != null) {
                          message += ('</br>' + response.Message)
                      }
                      alert(message);
                      return;
                  }
              });
          }
          else {
              alert('没有修改！');
              return;
          }
      }
      function reject() {
          $('#menuText').val('');
          $('#menuCode').val('');
          appendMenuList.splice(0, appendMenuList.length);
          editMenuList.splice(0, editMenuList.length);
          removeMenuList.splice(0, removeMenuList.length);
          $('#treeArchiveMenu').tree('reload');
          status = '';
      }
      function ok() {
          if (status == 'append') {
              var node = $('#treeArchiveMenu').tree('getSelected');
              if (node == null) {
                  alert('请先选择一个节点！');
                  return;
              }
              if (!validateInput()) {
                  return;
              }
              var menuText = $('#menuText').val();
              var menuCode = $('#menuCode').val();
              var id = new Date().getTime();
              $('#treeArchiveMenu').tree('append', {
                  parent: (node ? node.target : null),
                  data: [{
                      id: id,
                      text: menuText,
                      attributes: { typeCode: menuCode }
                  }]
              });
              var menu = new Object;
              menu.TypeID = id;
              menu.ArchiveMenu = menuText;
              menu.ParentID = node.id;
              menu.TypeCode = menuCode;
              appendMenuList.push(menu);
              var appendNode = $('#treeArchiveMenu').tree('find', id);
              if (node != null) {
                  $('#treeArchiveMenu').tree('select', appendNode.target);
              }
              return;
          }
          if (status == 'edit') {
              var node = $('#treeArchiveMenu').tree('getSelected');
              if (node == null) {
                  alert('请先选择一个节点！');
                  return;
              }
              if (!validateInput()) {
                  return;
              }
              var menuText = $('#menuText').val();
              var menuCode = $('#menuCode').val();
              node.text = menuText;
              $('#treeArchiveMenu').tree('update', node);
              for (i = 0; i < appendMenuList.length; i++) {
                  if (appendMenuList[i].TypeID = node.id) {
                      appendMenuList[i].ArchiveMenu = menuText;
                      appendMenuList[i].TypeCode = menuCode;
                      return;
                  }
              }
              for (i = 0; i < editMenuList.length; i++) {
                  if (editMenuList[i].TypeID = node.id) {
                      editMenuList[i].ArchiveMenu = menuText;
                      editMenuList[i].TypeCode = menuCode;
                      return;
                  }
              }
              var pid = "0";
              var pnode = $('#treeArchiveMenu').tree('getParent', node.target);
              if (pnode != null) {
                  pid = pnode.id;
              }
              var menu = new Object;
              menu.TypeID = node.id;
              menu.ArchiveMenu = menuText;
              menu.ParentID = pid;
              menu.TypeCode = menuCode;
              editMenuList.push(menu);
              return;
          }
      }
      function cancel() {
          var node = $('#treeArchiveMenu').tree('getSelected');
          if (node) {
              $('#menuText').val(node.text);
              $('#menuCode').val(node.attributes.typeCode);
          }
          else {
              $('#menuText').val('');
              $('#menuCode').val('');
          }
          $('#menuTextValidateMsg').hide();
          $('#menuCodeValidateMsg').hide();
          $('#btnAppend').removeAttr('disabled');
          $('#btnEdit').removeAttr('disabled');
          $('#btnRemove').removeAttr('disabled');
          $('#btnCommit').removeAttr('disabled');
          $('#btnReject').removeAttr('disabled');
          status = '';
      }
      function validateInput() {
          var validateResult = true;
          var menuText = $('#menuText').val();
          var menuCode = $('#menuCode').val();
          if (menuText == null || menuText == '') {
              $('#menuTextValidateMsg').show();
              validateResult = false;
          }
          if (menuCode == null || menuCode == '') {
              $('#menuCodeValidateMsg').show();
             validateResult = false;
         }
         if (!validateResult) {
             return false;
         }
         $('#menuTextValidateMsg').hide();
         $('#menuCodeValidateMsg').hide();
         return true;
      }
  </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div style="width:300px;height:auto;overflow:auto;position:relative;left:15px;top:15px;float:left;">
        <ul id="treeArchiveMenu"></ul>
    </div>
    <div style="position:relative;left:100px;top:100px;">
        <button type="button" id="btnAppend" class="sexybutton" onclick="append();"><span><span><span class="add">新增</span></span></span></button> 
        <button type="button" id="btnRemove" class="sexybutton" onclick="remove();"><span><span><span class="delete">移除</span></span></span></button> 
        <button type="button" id="btnCommit" class="sexybutton" onclick="commit();"><span><span><span class="save">提交更改</span></span></span></button>  
        <button type="button" id="btnReject" class="sexybutton" onclick="reject();"><span><span><span class="export">撤销更改</span></span></span></button><br/>
        <span>编目名称（必填）：</span> 
        <input id="menuText"/><span style="color:Red;">*</span><span id="menuTextValidateMsg" style="color:Red;display:none;">必须填写编目名称</span><br/>
        <span>编目代码（必填）：</span>  
        <input id="menuCode"/><span style="color:Red;">*</span><span id="menuCodeValidateMsg" style="color:Red;display:none;">必须填写编目代码</span><br/>
        <button type="button" id="btnOK" class="sexybutton" onclick="ok();"><span><span><span class="add">确定</span></span></span></button> 
        <button type="button" id="btnCancel" class="sexybutton" onclick="cancel();"><span><span><span class="cancel">取消</span></span></span></button>           
    </div>
</asp:Content>

